<?php

namespace bee;

/**
 *
 * @author dzelenika
 * @todo treba doraditi dispatching iste metoda za dva objekta (često, ali ne mora iste klase)
 */
class dispatcher extends singleton
{ public $events;
  private $objects;
  public $dispatchAuto;
  static protected $__instance;
  //static protected $__class;

  protected function __construct()
  { $this->events         = array();
    $this->objects        = array();
    $this->dispatchAuto   = config::getInstance()->sys_DISPATCH_LEVEL;
  }
  /**
   *
   * @param $event
   * @param $method
   * @param $object
   */
  public function addEvent($event, $method, object &$object = null)
  { $this->events[$event]["method"] = $method;
    if($object != null)
    { $this->events[$event]["object"] = $object;
    }
  }
  /**
   *
   * @param object $object
   */
  public function addobject($object)
  { if(!in_array($object, $this->objects))
    { $this->objects[] = $object;
      if($object)
      foreach($object as $item)
      { if(is_a($item, "bee\\object"))
        { //echo ", %" . get_class($item) . "% ";
          if($item->__withEvents == true)
          {
            //echo "ADDED";
            $this->addobject($item);
          } else
          {

          }
        }
      }
    } else
    { //echo "ALLERADY IN DISPATH TABLE";

    }
  }
  /**
   *
   * @param string $event
   * @param array $args
   * @param bool $singleDispatch
   */
  public function dispatch($event, $args = null, $singleDispatch = false)
  {
    append2log("Dispatching event: $event:");
    $found = 0;
    if(!$event)
  	{  return;
  	}
  	if(!$args)
    { $args = $_REQUEST;
    }
    if($this->dispatchAuto & DISPATCH_AUTO_OBJECTS)
    { foreach($this->objects as &$object)
      { //echo "\n<br />AUTO OBJECT " . get_class($object);// . "\n<br />";
        if(method_exists($object, $event))
        { //echo "\n<br />AUTO OBJECT " . get_class($object);// . "\n<br />";
          $found++;
          $object->$event($args);
          //echo " :: " . $event;
          if($singleDispatch) return;
        }
      }
    }
    if($this->dispatchAuto & DISPATCH_AUTO_FUNCTIONS)
    { if(function_exists($event))
      { //echo " AUTO FUNCTION " . get_class($object);
        $found++;
        $event($args);
        if($singleDispatch) return;
      }
    }
  	if(isset($this->events[$event]))
    { $method = $this->events[$event]["method"];
  	  if(isset($this->events[$event]["object"]))
  	  {	//echo " REGISTERED OBJECT " . get_class($object);
  	    $found++;
  	    $object = $this->events[$event]["object"];
  	    $ro = new \ReflectionObject($object);
  		  $object->$method($args);
  		  return;
      } else
      { $method($args);
        return;
      }
    }

    //echo( " ... $found EVENTS FOUND");
    //if($event == "drawForm"){die("stop DF");}
    return;
  }
  public function getDispachObjects()
  {
  	return $this->objects;
  }
}